!
!  Input     Parser of an input file.
!
!  Copyright © 1998-2010 F.Hroch (hroch@physics.muni.cz)
!
!  This file is part of Munipack.
!
!  Munipack is free software: you can redistribute it and/or modify
!  it under the terms of the GNU General Public License as published by
!  the Free Software Foundation, either version 3 of the License, or
!  (at your option) any later version.
!  
!  Munipack is distributed in the hope that it will be useful,
!  but WITHOUT ANY WARRANTY; without even the implied warranty of
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!  GNU General Public License for more details.
!  
!  You should have received a copy of the GNU General Public License
!  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.


module input

  ! Handle na .opt file

  implicit none

!  integer, parameter :: opt_ok = 0, opt_end = -1, opt_err = 1
  integer, parameter :: input_unknown = -1, input_comment = 0, input_key = 1, &
       input_data = 2, input_empty = 3

contains

  subroutine input_rcomment(record,line)

    character(len=*), intent(in) :: record
    character(len=*), intent(out) :: line
    integer :: c

    c = index(record,'#')

    if( c > 0 ) then
!       c = c - 1
!       line = record(:c)
       line = record(:c-1)
    else
       line = record
!       c = len_trim(line)
    endif

 !   line = record(:c)

  end subroutine input_rcomment


  function input_classify(line)

    integer :: input_classify
    character(len=*), intent(in) :: line
!    integer :: ir

    input_classify = input_unknown

    if( index(line,'=') > 0 ) then
       input_classify = input_key
    else if( line == '' ) then
       input_classify = input_empty
    else
       input_classify = input_data
    end if

  end function input_classify


  subroutine input_keys(line,key,value)

!    integer, intent(in) :: n
!    integer, intent(out) :: i
    character(len=*), intent(in) :: line
    character(len=*), intent(out) :: key,value
!    character(len=81) :: line
    integer :: ir

!    do 
 !      read(*,'(A)',iostat=i,err=666,end=99) line

!       c = index(line,'!') + index(line,'#')
!       if( c > 0 ) then
!          c = c - 1
!       else
!          c = len_trim(line)
!       endif
       ir = index(line,'=')
       if( ir > 0 ) then
          key = adjustl(line(:ir-1))
          value = adjustl(line(ir+1:))
       else
          key = ''
          value = ''
       endif
 !   enddo

!99  continue
!    i = opt_end
!    return

!666 continue
!    i = opt_err

!  end subroutine input_read
     end subroutine input_keys


   end module input
